package com.fzj.example.code;

import sun.misc.Unsafe;

import java.io.UnsupportedEncodingException;

/**
 * ClassName: CodeParse
 * date: 2019/10/10 17:16
 * Description: TODO
 *
 * @author fanzj@bonree.com
 * @since JDK 1.8
 */
public class CodeParse {
    public static void main(String[] args) throws UnsupportedEncodingException {
        //"中文".getBytes("gbk"),以GBK编码，
        //new String("中文".getBytes("gbk"),"gbk"),解码必须是GBK，字符串一定是unicode编码
        //字符串默认都是unicode编码，String是对象，不是字符串，他是存储对象的
        //new String(byte[] byte)是一个StringCoding.decode过程，也就是，String对象过程是
        //"中文".getBytes("gbk")是编码过程
        //new String(bytes,m)是一个以m方式解码过程
        //new String(bytes)是一个以默认方式UTF-8解码过程，JVM参数是-Dfile.encoding
        String M1="中文";
        String M2=new String("中文");
        //默认编解码都是UTF-8
        String M11=new String("中文".getBytes());
        //编码gbk,解码默认是UTF-8
        String M3=new String("中文".getBytes("gbk"));
        //编码gbk,解码配置是UTF-8，于M3一样
        String M4=new String("中文".getBytes("gbk"),"UTF-8");
        //M3,M4是无法处理的，经过GBK编码之后，byte字节无法在UTF-8字符集里做解码识别了，都变成了？？？？了
        //编码UTF-8,解码配置是gbk
        String M5=new String("中文".getBytes(),"gbk");
        System.out.println("M1:"+M1);
        System.out.println("M11:"+M11);
        System.out.println("M2:"+M2);
        System.out.println("M3:"+M3);
        System.out.println("M4:"+M4);
        System.out.println("M5:"+M5);
        System.out.println("M1:"+M1.getBytes().length);
        System.out.println("M11:"+M11.getBytes().length);
        System.out.println("M2:"+M2.getBytes().length);
        System.out.println("M3:"+M3.getBytes().length);
        System.out.println("M4:"+M4.getBytes().length);
        System.out.println("M5:"+M5.getBytes().length);

        M3=new String(M3.getBytes(),"gbk");
        M5=new String(M5.getBytes("gbk"));
        System.out.println("M3:"+M3);
        System.out.println("M5:"+M5);
        System.out.println("M5:"+M5.getBytes().length);


        String str=new String("中文".getBytes("gbk"),"gbk");

        // ThreadLocalCoders.decoderFor("UTF-8"),所以str.getBytes()默认是UTF-8
        System.out.println(str.getBytes());
        System.out.println("默认 length:"+str.getBytes().length);
        System.out.println("UTF-8 length:"+str.getBytes("utf-8").length);
        System.out.println("GBK length:"+str.getBytes("gbk").length);

        //

    }
}
